<!DOCTYPE html>


<html lang="zh-CN">


<head>
  <meta charset="utf-8" />
    
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" />
  <title>
    6 个JVM性能监控、调优工具使用详解.md |  
  </title>
  <meta name="generator" content="hexo-theme-ayer">
  
  <link rel="shortcut icon" href="/favicon.ico" />
  
  
<link rel="stylesheet" href="/dist/main.css">

  
<link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/Shen-Yu/cdn/css/remixicon.min.css">

  
<link rel="stylesheet" href="/css/custom.css">

  
  
<script src="https://cdn.jsdelivr.net/npm/pace-js@1.0.2/pace.min.js"></script>

  
  

  

</head>

</html>

<body>
  <div id="app">
    
      
    <main class="content on">
      <section class="outer">
  <article
  id="post-tips/6 个JVM性能监控、调优工具使用详解"
  class="article article-type-post"
  itemscope
  itemprop="blogPost"
  data-scroll-reveal
>
  <div class="article-inner">
    
    <header class="article-header">
       
<h1 class="article-title sea-center" style="border-left:0" itemprop="name">
  6 个JVM性能监控、调优工具使用详解.md
</h1>
 

    </header>
     
    <div class="article-meta">
      <a href="/2020/11/11/tips/6%20%E4%B8%AAJVM%E6%80%A7%E8%83%BD%E7%9B%91%E6%8E%A7%E3%80%81%E8%B0%83%E4%BC%98%E5%B7%A5%E5%85%B7%E4%BD%BF%E7%94%A8%E8%AF%A6%E8%A7%A3/" class="article-date">
  <time datetime="2020-11-10T16:00:00.000Z" itemprop="datePublished">2020-11-11</time>
</a> 
  <div class="article-category">
    <a class="article-category-link" href="/categories/tips/">tips</a>
  </div>
  
<div class="word_count">
    <span class="post-time">
        <span class="post-meta-item-icon">
            <i class="ri-quill-pen-line"></i>
            <span class="post-meta-item-text"> 字数统计:</span>
            <span class="post-count">3k</span>
        </span>
    </span>

    <span class="post-time">
        &nbsp; | &nbsp;
        <span class="post-meta-item-icon">
            <i class="ri-book-open-line"></i>
            <span class="post-meta-item-text"> 阅读时长≈</span>
            <span class="post-count">14 分钟</span>
        </span>
    </span>
</div>
 
    </div>
      
    <div class="tocbot"></div>




  
    <div class="article-entry" itemprop="articleBody">
       
  <h1 id="6-个JVM性能监控、调优工具使用详解"><a href="#6-个JVM性能监控、调优工具使用详解" class="headerlink" title="6 个JVM性能监控、调优工具使用详解"></a>6 个JVM性能监控、调优工具使用详解</h1><p>现实企业级Java应用开发、维护中，有时候我们会碰到下面这些问题：  </p>
<ul>
<li><p>OutOfMemoryError，内存不足</p>
</li>
<li><p>内存泄露</p>
</li>
<li><p>线程死锁</p>
</li>
<li><p>锁争用（Lock Contention）</p>
</li>
<li><p>Java进程消耗CPU过高</p>
</li>
<li><p>……</p>
</li>
</ul>
<p>    这些问题在日常开发、维护中可能被很多人忽视（比如有的人遇到上面的问题只是重启服务器或者调大内存，而不会深究问题根源），但能够理解并解决这些问题是Java程序员进阶的必备要求。本文将对一些常用的JVM性能调优监控工具进行介绍，希望能起抛砖引玉之用。</p>
<p><strong>而且这些监控、调优工具的使用，无论你是运维、开发、测试，都是必须掌握的。</strong></p>
<p><strong>1、 jps (Java Virtual Mac**</strong>hine Process Status Tool)** </p>
<p>    jps主要用来输出JVM中运行的进程状态信息。语法格式如下：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">jps [options] [hostid]</span><br></pre></td></tr></table></figure>

<p>    如果不指定hostid就默认为当前主机或服务器。</p>
<p>    命令行参数选项说明如下：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">-q 不输出类名、Jar名和传入main方法的参数  </span><br><span class="line">  </span><br><span class="line">-m 输出传入main方法的参数  </span><br><span class="line">  </span><br><span class="line">-l 输出main类或Jar的全限名  </span><br><span class="line">  </span><br><span class="line">-v 输出传入JVM的参数</span><br></pre></td></tr></table></figure>

<p>   比如下面：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">root@ubuntu:&#x2F;# jps -m -l  </span><br><span class="line">2458 org.artifactory.standalone.main.Main &#x2F;usr&#x2F;local&#x2F;artifactory-2.2.5&#x2F;etc&#x2F;jetty.xml  </span><br><span class="line">29920 com.sun.tools.hat.Main -port 9998 &#x2F;tmp&#x2F;dump.dat  </span><br><span class="line">3149 org.apache.catalina.startup.Bootstrap start  </span><br><span class="line">30972 sun.tools.jps.Jps -m -l  </span><br><span class="line">8247 org.apache.catalina.startup.Bootstrap start  </span><br><span class="line">25687 com.sun.tools.hat.Main -port 9999 dump.dat  </span><br><span class="line">21711 mrf-center.jar  </span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p><strong>2、 jstack</strong></p>
<p>    jstack主要用来查看某个Java进程内的线程堆栈信息。语法格式如下：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">jstack [option] pid  </span><br><span class="line">jstack [option] executable core  </span><br><span class="line">jstack [option] [server-id@]remote-hostname-or-ip</span><br></pre></td></tr></table></figure>

<p>    命令行参数选项说明如下：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">-l long listings，会打印出额外的锁信息，在发生死锁时可以用jstack -l pid来观察锁持有情况-m mixed mode，不仅会输出Java堆栈信息，还会输出C&#x2F;C++堆栈信息（比如Native方法）</span><br></pre></td></tr></table></figure>

<p>    jstack可以定位到线程堆栈，根据堆栈信息我们可以定位到具体代码，所以它在JVM性能调优中使用得非常多。下面我们来一个实例找出某个Java进程中最耗费CPU的Java线程并定位堆栈信息，用到的命令有ps、top、printf、jstack、grep。</p>
<p>    第一步先找出Java进程ID，我部署在服务器上的Java应用名称为mrf-center：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">root@ubuntu:&#x2F;# ps -ef | grep mrf-center | grep -v grep  </span><br><span class="line">root     21711     1  1 14:47 pts&#x2F;3    00:02:10 java -jar mrf-center.jar</span><br></pre></td></tr></table></figure>

<p>    得到进程ID为21711，第二步找出该进程内最耗费CPU的线程，可以使用ps -Lfp pid或者ps -mp pid -o THREAD, tid, time或者top -Hp pid，我这里用第三个，输出如下：</p>
<p><img src=""></p>
<p>    TIME列就是各个Java线程耗费的CPU时间，CPU时间最长的是线程ID为21742的线程，用</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">printf &quot;%x\n&quot; 21742</span><br></pre></td></tr></table></figure>

<p>    得到21742的十六进制值为54ee，下面会用到。   </p>
<p>    OK，下一步终于轮到jstack上场了，它用来输出进程21711的堆栈信息，然后根据线程ID的十六进制值grep，如下：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">root@ubuntu:&#x2F;# jstack 21711 | grep 54ee  </span><br><span class="line">&quot;PollIntervalRetrySchedulerThread&quot; prio&#x3D;10 tid&#x3D;0x00007f950043e000 nid&#x3D;0x54ee in Object.wait() [0x00007f94c6eda000]</span><br></pre></td></tr></table></figure>

<p>    可以看到CPU消耗在PollIntervalRetrySchedulerThread这个类的Object.wait()，我找了下我的代码，定位到下面的代码：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">&#x2F;&#x2F; Idle wait  </span><br><span class="line">getLog().info(&quot;Thread [&quot; + getName() + &quot;] is idle waiting...&quot;);  </span><br><span class="line">schedulerThreadState &#x3D; PollTaskSchedulerThreadState.IdleWaiting;  </span><br><span class="line">long now &#x3D; System.currentTimeMillis();  </span><br><span class="line">long waitTime &#x3D; now + getIdleWaitTime();  </span><br><span class="line">long timeUntilContinue &#x3D; waitTime - now;  </span><br><span class="line">synchronized(sigLock) &#123;	try &#123;  </span><br><span class="line">    	if(!halted.get()) &#123;  </span><br><span class="line">    		sigLock.wait(timeUntilContinue);  </span><br><span class="line">    	&#125;  </span><br><span class="line">    &#125; 	catch (InterruptedException ignore) &#123;  </span><br><span class="line">    &#125;  </span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>    它是轮询任务的空闲等待代码，上面的sigLock.wait(timeUntilContinue)就对应了前面的Object.wait()。</p>
<p><strong>3、 jmap（Memory Map）和jhat（Java Heap Analysis Tool）</strong></p>
<p>    jmap用来查看堆内存使用状况，一般结合jhat使用。</p>
<p>    jmap语法格式如下：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">jmap [option] pid  </span><br><span class="line">jmap [option] executable core  </span><br><span class="line">jmap [option] [server-id@]remote-hostname-or-ip</span><br></pre></td></tr></table></figure>

<p>    如果运行在64位JVM上，可能需要指定-J-d64命令选项参数。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">jmap -permstat pid</span><br></pre></td></tr></table></figure>

<p>    打印进程的类加载器和类加载器加载的持久代对象信息，输出：类加载器名称、对象是否存活（不可靠）、对象地址、父类加载器、已加载的类大小等信息，如下图：</p>
<p><img src=""></p>
<p>   使用jmap -heap pid查看进程堆内存使用情况，包括使用的GC算法、堆配置参数和各代中堆内存使用情况。比如下面的例子：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br></pre></td><td class="code"><pre><span class="line">root@ubuntu:&#x2F;# jmap -heap 21711  </span><br><span class="line">Attaching to process ID 21711, please wait...  </span><br><span class="line">Debugger attached successfully.  </span><br><span class="line">Server compiler detected.  </span><br><span class="line">JVM version is 20.10-b01  </span><br><span class="line">  </span><br><span class="line">using thread-local object allocation.  </span><br><span class="line">Parallel GC with 4 thread(s)  </span><br><span class="line">  </span><br><span class="line">Heap Configuration:  </span><br><span class="line">MinHeapFreeRatio &#x3D; 40     </span><br><span class="line">MaxHeapFreeRatio &#x3D; 70     </span><br><span class="line">MaxHeapSize      &#x3D; 2067791872 (1972.0MB)  </span><br><span class="line">NewSize          &#x3D; 1310720 (1.25MB)  </span><br><span class="line">MaxNewSize       &#x3D; 17592186044415 MB  </span><br><span class="line">OldSize          &#x3D; 5439488 (5.1875MB)  </span><br><span class="line">NewRatio         &#x3D; 2     </span><br><span class="line">SurvivorRatio    &#x3D; 8     </span><br><span class="line">PermSize         &#x3D; 21757952 (20.75MB)  </span><br><span class="line">MaxPermSize      &#x3D; 85983232 (82.0MB)  </span><br><span class="line">  </span><br><span class="line">Heap Usage:  </span><br><span class="line">PS Young Generation  </span><br><span class="line">Eden Space:  </span><br><span class="line">   capacity &#x3D; 6422528 (6.125MB)  </span><br><span class="line">   used     &#x3D; 5445552 (5.1932830810546875MB)  </span><br><span class="line">   free     &#x3D; 976976 (0.9317169189453125MB)  </span><br><span class="line">   84.78829520089286% used  </span><br><span class="line">From Space:  </span><br><span class="line">   capacity &#x3D; 131072 (0.125MB)  </span><br><span class="line">   used     &#x3D; 98304 (0.09375MB)  </span><br><span class="line">   free     &#x3D; 32768 (0.03125MB)  </span><br><span class="line">   75.0% used  </span><br><span class="line">To Space:  </span><br><span class="line">   capacity &#x3D; 131072 (0.125MB)  </span><br><span class="line">   used     &#x3D; 0 (0.0MB)  </span><br><span class="line">   free     &#x3D; 131072 (0.125MB)  </span><br><span class="line">   0.0% used  </span><br><span class="line">PS Old Generation  </span><br><span class="line">   capacity &#x3D; 35258368 (33.625MB)  </span><br><span class="line">   used     &#x3D; 4119544 (3.9287033081054688MB)  </span><br><span class="line">   free     &#x3D; 31138824 (29.69629669189453MB)  </span><br><span class="line">   11.683876009235595% used  </span><br><span class="line">PS Perm Generation  </span><br><span class="line">   capacity &#x3D; 52428800 (50.0MB)  </span><br><span class="line">   used     &#x3D; 26075168 (24.867218017578125MB)  </span><br><span class="line">   free     &#x3D; 26353632 (25.132781982421875MB)  </span><br><span class="line">   49.73443603515625% used  </span><br><span class="line">   ....</span><br></pre></td></tr></table></figure>

<p>    使用jmap -histo[:live] pid查看堆内存中的对象数目、大小统计直方图，如果带上live则只统计活对象，如下：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br></pre></td><td class="code"><pre><span class="line">root@ubuntu:&#x2F;# jmap -histo:live 21711 | more   </span><br><span class="line">num     #instances         #bytes  class name----------------------------------------------  </span><br><span class="line">   1:         38445        5597736  &lt;constMethodKlass&gt;  </span><br><span class="line">   2:         38445        5237288  &lt;methodKlass&gt;  </span><br><span class="line">   3:          3500        3749504  &lt;constantPoolKlass&gt;  </span><br><span class="line">   4:         60858        3242600  &lt;symbolKlass&gt;  </span><br><span class="line">   5:          3500        2715264  &lt;instanceKlassKlass&gt;  </span><br><span class="line">   6:          2796        2131424  &lt;constantPoolCacheKlass&gt;  </span><br><span class="line">   7:          5543        1317400  [I  </span><br><span class="line">   8:         13714        1010768  [C  </span><br><span class="line">   9:          4752        1003344  [B  </span><br><span class="line">  10:          1225         639656  &lt;methodDataKlass&gt;  </span><br><span class="line">  11:         14194         454208  java.lang.String  </span><br><span class="line">  12:          3809         396136  java.lang.Class  </span><br><span class="line">  13:          4979         311952  [S  </span><br><span class="line">  14:          5598         287064  [[I  </span><br><span class="line">  15:          3028         266464  java.lang.reflect.Method  </span><br><span class="line">  16:           280         163520  &lt;objArrayKlassKlass&gt;  </span><br><span class="line">  17:          4355         139360  java.util.HashMap$Entry  </span><br><span class="line">  18:          1869         138568  [Ljava.util.HashMap$Entry;  </span><br><span class="line">  19:          2443          97720  java.util.LinkedHashMap$Entry  </span><br><span class="line">  20:          2072          82880  java.lang.ref.SoftReference  </span><br><span class="line">  21:          1807          71528  [Ljava.lang.Object;  </span><br><span class="line">  22:          2206          70592  java.lang.ref.WeakReference  </span><br><span class="line">  23:           934          52304  java.util.LinkedHashMap  </span><br><span class="line">  24:           871          48776  java.beans.MethodDescriptor  </span><br><span class="line">  25:          1442          46144  java.util.concurrent.ConcurrentHashMap$HashEntry  </span><br><span class="line">  26:           804          38592  java.util.HashMap  </span><br><span class="line">  27:           948          37920  java.util.concurrent.ConcurrentHashMap$Segment  </span><br><span class="line">  28:          1621          35696  [Ljava.lang.Class;  </span><br><span class="line">  29:          1313          34880  [Ljava.lang.String;  </span><br><span class="line">  30:          1396          33504  java.util.LinkedList$Entry  </span><br><span class="line">  31:           462          33264  java.lang.reflect.Field  </span><br><span class="line">  32:          1024          32768  java.util.Hashtable$Entry  </span><br><span class="line">  33:           948          31440  [Ljava.util.concurrent.ConcurrentHashMap$HashEntry;</span><br></pre></td></tr></table></figure>

<p>    class name是对象类型，说明如下：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">B  byte  </span><br><span class="line">C  char  </span><br><span class="line">D  double  </span><br><span class="line">F  float  </span><br><span class="line">I  int  </span><br><span class="line">J  long  </span><br><span class="line">Z  boolean  </span><br><span class="line">[  数组，如[I表示int[]  </span><br><span class="line">[L+类名 其他对象</span><br></pre></td></tr></table></figure>

<p>    还有一个很常用的情况是：用jmap把进程内存使用情况dump到文件中，再用jhat分析查看。jmap进行dump命令格式如下：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">jmap -dump:format&#x3D;b,file&#x3D;dumpFileName pid</span><br></pre></td></tr></table></figure>

<p>    我一样地对上面进程ID为21711进行Dump：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">root@ubuntu:&#x2F;# jmap -dump:format&#x3D;b,file&#x3D;&#x2F;tmp&#x2F;dump.dat 21711       </span><br><span class="line">Dumping heap to &#x2F;tmp&#x2F;dump.dat ...  </span><br><span class="line">Heap dump file created</span><br></pre></td></tr></table></figure>

<p>   dump出来的文件可以用MAT、VisualVM等工具查看，这里用jhat查看：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">root@ubuntu:&#x2F;# jhat -port 9998 &#x2F;tmp&#x2F;dump.dat  </span><br><span class="line">Reading from &#x2F;tmp&#x2F;dump.dat...  </span><br><span class="line">Dump file created Tue Jan 28 17:46:14 CST 2014Snapshot read, resolving...  </span><br><span class="line">Resolving 132207 objects...  </span><br><span class="line">Chasing references, expect 26 dots..........................  </span><br><span class="line">Eliminating duplicate references..........................  </span><br><span class="line">Snapshot resolved.  </span><br><span class="line">Started HTTP server on port 9998Server is ready.</span><br></pre></td></tr></table></figure>

<p>     注意如果Dump文件太大，可能需要加上-J-Xmx512m这种参数指定最大堆内存，即jhat -J-Xmx512m -port 9998 /tmp/dump.dat。然后就可以在浏览器中输入主机地址:9998查看了：</p>
<p><img src=""></p>
<p>    上面红线框出来的部分大家可以自己去摸索下，最后一项支持OQL（对象查询语言）。</p>
<p><strong>4、jstat（JVM统计监测工具）</strong></p>
<p>    语法格式如下：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">jstat [ generalOption | outputOptions vmid [interval[s|ms] [count]] ]</span><br></pre></td></tr></table></figure>

<p>    vmid是Java虚拟机ID，在Linux/Unix系统上一般就是进程ID。interval是采样时间间隔。count是采样数目。比如下面输出的是GC信息，采样时间间隔为250ms，采样数为4：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">root@ubuntu:&#x2F;# jstat -gc 21711 250 4   </span><br><span class="line">S0C    S1C    S0U    S1U      EC       EU        OC         OU       PC     PU    YGC     YGCT    FGC    FGCT     GCT     </span><br><span class="line">192.0  192.0   64.0   0.0    6144.0   1854.9   32000.0     4111.6   55296.0 25472.7    702    0.431   3      0.218    0.649  </span><br><span class="line">192.0  192.0   64.0   0.0    6144.0   1972.2   32000.0     4111.6   55296.0 25472.7    702    0.431   3      0.218    0.649  </span><br><span class="line">192.0  192.0   64.0   0.0    6144.0   1972.2   32000.0     4111.6   55296.0 25472.7    702    0.431   3      0.218    0.649  </span><br><span class="line">192.0  192.0   64.0   0.0    6144.0   2109.7   32000.0     4111.6   55296.0 25472.7    702    0.431   3      0.218    0.649  </span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p>    要明白上面各列的意义，先看JVM堆内存布局：</p>
<p><img src=""></p>
<p>    可以看出：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">堆内存 &#x3D; 年轻代 + 年老代 + 永久代  </span><br><span class="line">年轻代 &#x3D; Eden区 + 两个Survivor区（From和To）</span><br></pre></td></tr></table></figure>

<p>    现在来解释各列含义：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">S0C、S1C、S0U、S1U：Survivor 0&#x2F;1区容量（Capacity）和使用量（Used）  </span><br><span class="line">EC、EU：Eden区容量和使用量  </span><br><span class="line">OC、OU：年老代容量和使用量  </span><br><span class="line">PC、PU：永久代容量和使用量  </span><br><span class="line">YGC、YGT：年轻代GC次数和GC耗时  </span><br><span class="line">FGC、FGCT：Full GC次数和Full GC耗时  </span><br><span class="line">GCT：GC总耗时</span><br></pre></td></tr></table></figure>

<p><strong>5、hprof（Heap/CPU Profiling Tool）</strong></p>
<p>    hprof能够展现CPU使用率，统计堆内存使用情况。</p>
<p>    语法格式如下：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">java -agentlib:hprof[&#x3D;options] ToBeProfiledClass  </span><br><span class="line">java -Xrunprof[:options] ToBeProfiledClass  </span><br><span class="line">javac -J-agentlib:hprof[&#x3D;options] ToBeProfiledClass</span><br></pre></td></tr></table></figure>

<p>    完整的命令选项如下：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line">Option Name and Value  Description                    Default  </span><br><span class="line">---------------------  -----------                    -------  </span><br><span class="line">heap&#x3D;dump|sites|all    heap profiling                 all  </span><br><span class="line">cpu&#x3D;samples|times|old  CPU usage                      off  </span><br><span class="line">monitor&#x3D;y|n            monitor contention             n  </span><br><span class="line">format&#x3D;a|b             text(txt) or binary output     a  </span><br><span class="line">file&#x3D;&lt;file&gt;            write data to file             java.hprof[.txt]  </span><br><span class="line">net&#x3D;&lt;host&gt;:&lt;port&gt;      send data over a socket        off  </span><br><span class="line">depth&#x3D;&lt;size&gt;           stack trace depth              4  </span><br><span class="line">interval&#x3D;&lt;ms&gt;          sample interval in ms          10  </span><br><span class="line">cutoff&#x3D;&lt;value&gt;         output cutoff point            0.0001  </span><br><span class="line">lineno&#x3D;y|n             line number in traces?         y  </span><br><span class="line">thread&#x3D;y|n             thread in traces?              n  </span><br><span class="line">doe&#x3D;y|n                dump on exit?                  y  </span><br><span class="line">msa&#x3D;y|n                Solaris micro state accounting n  </span><br><span class="line">force&#x3D;y|n              force output to &lt;file&gt;         y  </span><br><span class="line">verbose&#x3D;y|n            print messages about dumps     y</span><br></pre></td></tr></table></figure>

<p>    来几个官方指南上的实例。</p>
<p>    CPU Usage Sampling Profiling(cpu=samples)的例子：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">java -agentlib:hprof&#x3D;cpu&#x3D;samples,interval&#x3D;20,depth&#x3D;3 Hello</span><br></pre></td></tr></table></figure>

<p>    上面每隔20毫秒采样CPU消耗信息，堆栈深度为3，生成的profile文件名称是java.hprof.txt，在当前目录。 </p>
<p>    CPU Usage Times Profiling(cpu=times)的例子，它相对于CPU Usage Sampling Profile能够获得更加细粒度的CPU消耗信息，能够细到每个方法调用的开始和结束，它的实现使用了字节码注入技术（BCI）：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">javac -J-agentlib:hprof&#x3D;cpu&#x3D;times Hello.java</span><br></pre></td></tr></table></figure>

<p>    Heap Allocation Profiling(heap=sites)的例子：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">javac -J-agentlib:hprof&#x3D;heap&#x3D;sites Hello.java</span><br></pre></td></tr></table></figure>

<p>    Heap Dump(heap=dump)的例子，它比上面的Heap Allocation Profiling能生成更详细的Heap Dump信息：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">javac -J-agentlib:hprof&#x3D;heap&#x3D;dump Hello.java</span><br></pre></td></tr></table></figure>

<p> <strong>虽然在JVM启动参数中加入-Xrunprof:heap=sites参数可以生成CPU/Heap Profile文件，但对JVM性能影响非常大，不建议在线上服务器环境使用。</strong></p>
<p>- END -</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure>


<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure>


<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"></span><br><span class="line"> 推荐阅读   </span><br><span class="line">  </span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure>


<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure>

<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure>


<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"></span><br><span class="line">[](http:&#x2F;&#x2F;mp.weixin.qq.com&#x2F;s?__biz&#x3D;MzAwNTM5Njk3Mw&#x3D;&#x3D;&amp;mid&#x3D;2247495068&amp;idx&#x3D;1&amp;sn&#x3D;585db2088cfb062907493d0d6d0239ae&amp;chksm&#x3D;9b1fed1eac6864085249762523411888c43302654eaf72285d27a88acbfd853981f549010cb1&amp;scene&#x3D;21#wechat_redirect)[让运维简单高效，轻松搞定运维管理平台](http:&#x2F;&#x2F;mp.weixin.qq.com&#x2F;s?__biz&#x3D;MzAwNTM5Njk3Mw&#x3D;&#x3D;&amp;mid&#x3D;2247495019&amp;idx&#x3D;1&amp;sn&#x3D;85401c9f75e69ca450cf4e79647c902f&amp;chksm&#x3D;9b1fede9ac6864ff2a343ed4eee0d57a7f520292699e100fb12fc4497898ca4f424d27f3e272&amp;scene&#x3D;21#wechat_redirect)</span><br><span class="line"></span><br><span class="line">[在项目实践中，进行了以下DevOps方案建设](http:&#x2F;&#x2F;mp.weixin.qq.com&#x2F;s?__biz&#x3D;MzAwNTM5Njk3Mw&#x3D;&#x3D;&amp;mid&#x3D;2247495068&amp;idx&#x3D;1&amp;sn&#x3D;585db2088cfb062907493d0d6d0239ae&amp;chksm&#x3D;9b1fed1eac6864085249762523411888c43302654eaf72285d27a88acbfd853981f549010cb1&amp;scene&#x3D;21#wechat_redirect)</span><br><span class="line"></span><br><span class="line">[如何在Kubernetes上部署MySQL数据库](http:&#x2F;&#x2F;mp.weixin.qq.com&#x2F;s?__biz&#x3D;MzAwNTM5Njk3Mw&#x3D;&#x3D;&amp;mid&#x3D;2247494865&amp;idx&#x3D;1&amp;sn&#x3D;23f1e0ec017546473c449f0f3a22c25c&amp;chksm&#x3D;9b1fec53ac6865452249cf9d75db25f6035d6731b20149ef3ec6175dd2ecb820eced9e981c08&amp;scene&#x3D;21#wechat_redirect)  </span><br><span class="line"></span><br><span class="line">[Jenkins vs GitLab CI：CI&#x2F;CD工具之战](http:&#x2F;&#x2F;mp.weixin.qq.com&#x2F;s?__biz&#x3D;MzAwNTM5Njk3Mw&#x3D;&#x3D;&amp;mid&#x3D;2247494732&amp;idx&#x3D;1&amp;sn&#x3D;1b9c64a1850ef57921d2de4833351662&amp;chksm&#x3D;9b1fecceac6865d87b9bdf31ea7095cadacc36f3868d15793708c2d9319174ca4f10030aaaa4&amp;scene&#x3D;21#wechat_redirect)</span><br><span class="line"></span><br><span class="line">[浅谈 Kubernetes 网络模型（CNI）](http:&#x2F;&#x2F;mp.weixin.qq.com&#x2F;s?__biz&#x3D;MzAwNTM5Njk3Mw&#x3D;&#x3D;&amp;mid&#x3D;2247494057&amp;idx&#x3D;1&amp;sn&#x3D;07caccf4296371897b8aa08fb91d164d&amp;chksm&#x3D;9b1fe92bac68603dbfd886f62836758733db413c3d37c46b4a724473050e15b7445dba2b3ae9&amp;scene&#x3D;21#wechat_redirect)  </span><br><span class="line"></span><br><span class="line">[5 分钟看懂 HTTP 3](http:&#x2F;&#x2F;mp.weixin.qq.com&#x2F;s?__biz&#x3D;MzAwNTM5Njk3Mw&#x3D;&#x3D;&amp;mid&#x3D;2247493306&amp;idx&#x3D;1&amp;sn&#x3D;9563ce36ab75c68f0b811203e4309cc6&amp;chksm&#x3D;9b1fe638ac686f2ee22d4014c79d619167a4218600dfd15dafbfb7bb1d6744f6f34cadbcdd89&amp;scene&#x3D;21#wechat_redirect)  </span><br><span class="line"></span><br><span class="line">[31天拿下K8s最具有含金量的CKA认证](http:&#x2F;&#x2F;mp.weixin.qq.com&#x2F;s?__biz&#x3D;MzAwNTM5Njk3Mw&#x3D;&#x3D;&amp;mid&#x3D;2247494787&amp;idx&#x3D;1&amp;sn&#x3D;7af40185c41a9a5adc2e29d24fc91a0e&amp;chksm&#x3D;9b1fec01ac686517a511357018b25c8ac9a9cff3d679f3d9ac2c35666f9ecd3849919710b043&amp;scene&#x3D;21#wechat_redirect) ![][img-4]  </span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure>


<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure>




<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">  </span><br><span class="line"></span><br></pre></td></tr></table></figure>


<p><img src=""></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">  </span><br><span class="line"></span><br><span class="line">点亮，服务器三年不宕机![](data:image&#x2F;gif;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVQImWNgYGBgAAAABQABh6FO1AAAAABJRU5ErkJggg&#x3D;&#x3D;)</span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure>

 
      <!-- reward -->
      
      <div id="reword-out">
        <div id="reward-btn">
          打赏
        </div>
      </div>
      
    </div>
    

    <!-- copyright -->
    
    <div class="declare">
      <ul class="post-copyright">
        <li>
          <i class="ri-copyright-line"></i>
          <strong>版权声明： </strong>
          
          本博客所有文章除特别声明外，著作权归作者所有。转载请注明出处！
          
        </li>
      </ul>
    </div>
    
    <footer class="article-footer">
       
<div class="share-btn">
      <span class="share-sns share-outer">
        <i class="ri-share-forward-line"></i>
        分享
      </span>
      <div class="share-wrap">
        <i class="arrow"></i>
        <div class="share-icons">
          
          <a class="weibo share-sns" href="javascript:;" data-type="weibo">
            <i class="ri-weibo-fill"></i>
          </a>
          <a class="weixin share-sns wxFab" href="javascript:;" data-type="weixin">
            <i class="ri-wechat-fill"></i>
          </a>
          <a class="qq share-sns" href="javascript:;" data-type="qq">
            <i class="ri-qq-fill"></i>
          </a>
          <a class="douban share-sns" href="javascript:;" data-type="douban">
            <i class="ri-douban-line"></i>
          </a>
          <!-- <a class="qzone share-sns" href="javascript:;" data-type="qzone">
            <i class="icon icon-qzone"></i>
          </a> -->
          
          <a class="facebook share-sns" href="javascript:;" data-type="facebook">
            <i class="ri-facebook-circle-fill"></i>
          </a>
          <a class="twitter share-sns" href="javascript:;" data-type="twitter">
            <i class="ri-twitter-fill"></i>
          </a>
          <a class="google share-sns" href="javascript:;" data-type="google">
            <i class="ri-google-fill"></i>
          </a>
        </div>
      </div>
</div>

<div class="wx-share-modal">
    <a class="modal-close" href="javascript:;"><i class="ri-close-circle-line"></i></a>
    <p>扫一扫，分享到微信</p>
    <div class="wx-qrcode">
      <img src="//api.qrserver.com/v1/create-qr-code/?size=150x150&data=http://example.com/2020/11/11/tips/6%20%E4%B8%AAJVM%E6%80%A7%E8%83%BD%E7%9B%91%E6%8E%A7%E3%80%81%E8%B0%83%E4%BC%98%E5%B7%A5%E5%85%B7%E4%BD%BF%E7%94%A8%E8%AF%A6%E8%A7%A3/" alt="微信分享二维码">
    </div>
</div>

<div id="share-mask"></div>  
  <ul class="article-tag-list" itemprop="keywords"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/tips/" rel="tag">tips</a></li></ul>

    </footer>
  </div>

   
  <nav class="article-nav">
    
      <a href="/2020/11/11/mysql/02.Dbeaver%E5%8A%9F%E8%83%BD%E5%BC%BA%E5%A4%A7%E8%BF%98%E5%85%8D%E8%B4%B9/" class="article-nav-link">
        <strong class="article-nav-caption">上一篇</strong>
        <div class="article-nav-title">
          
            02.Dbeaver功能强大还免费.md
          
        </div>
      </a>
    
    
      <a href="/2020/11/11/develop/Python%E5%8F%91%E9%80%81%E9%82%AE%E4%BB%B6%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86%E4%B8%8E%E4%BB%A3%E7%A0%81%E8%AE%B2%E8%A7%A3%EF%BC%81/" class="article-nav-link">
        <strong class="article-nav-caption">下一篇</strong>
        <div class="article-nav-title">Python发送邮件基础知识与代码讲解！.md</div>
      </a>
    
  </nav>

   
<!-- valine评论 -->
<div id="vcomments-box">
  <div id="vcomments"></div>
</div>
<script src="//cdn1.lncld.net/static/js/3.0.4/av-min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/valine@1.4.14/dist/Valine.min.js"></script>
<script>
  new Valine({
    el: "#vcomments",
    app_id: "",
    app_key: "",
    path: window.location.pathname,
    avatar: "monsterid",
    placeholder: "给我的文章加点评论吧~",
    recordIP: true,
  });
  const infoEle = document.querySelector("#vcomments .info");
  if (infoEle && infoEle.childNodes && infoEle.childNodes.length > 0) {
    infoEle.childNodes.forEach(function (item) {
      item.parentNode.removeChild(item);
    });
  }
</script>
<style>
  #vcomments-box {
    padding: 5px 30px;
  }

  @media screen and (max-width: 800px) {
    #vcomments-box {
      padding: 5px 0px;
    }
  }

  #vcomments-box #vcomments {
    background-color: #fff;
  }

  .v .vlist .vcard .vh {
    padding-right: 20px;
  }

  .v .vlist .vcard {
    padding-left: 10px;
  }
</style>

 
     
</article>

</section>
      <footer class="footer">
  <div class="outer">
    <ul>
      <li>
        Copyrights &copy;
        2015-2020
        <i class="ri-heart-fill heart_icon"></i> TzWind
      </li>
    </ul>
    <ul>
      <li>
        
        
        
        由 <a href="https://hexo.io" target="_blank">Hexo</a> 强力驱动
        <span class="division">|</span>
        主题 - <a href="https://github.com/Shen-Yu/hexo-theme-ayer" target="_blank">Ayer</a>
        
      </li>
    </ul>
    <ul>
      <li>
        
        
        <span>
  <span><i class="ri-user-3-fill"></i>访问人数:<span id="busuanzi_value_site_uv"></span></s>
  <span class="division">|</span>
  <span><i class="ri-eye-fill"></i>浏览次数:<span id="busuanzi_value_page_pv"></span></span>
</span>
        
      </li>
    </ul>
    <ul>
      
    </ul>
    <ul>
      
    </ul>
    <ul>
      <li>
        <!-- cnzz统计 -->
        
        <script type="text/javascript" src='https://s9.cnzz.com/z_stat.php?id=1278069914&amp;web_id=1278069914'></script>
        
      </li>
    </ul>
  </div>
</footer>
      <div class="float_btns">
        <div class="totop" id="totop">
  <i class="ri-arrow-up-line"></i>
</div>

<div class="todark" id="todark">
  <i class="ri-moon-line"></i>
</div>

      </div>
    </main>
    <aside class="sidebar on">
      <button class="navbar-toggle"></button>
<nav class="navbar">
  
  <div class="logo">
    <a href="/"><img src="/images/ayer-side.svg" alt="Hexo"></a>
  </div>
  
  <ul class="nav nav-main">
    
    <li class="nav-item">
      <a class="nav-item-link" href="/">主页</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/archives">归档</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/categories">分类</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/tags">标签</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" target="_blank" rel="noopener" href="http://www.baidu.com">百度</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/friends">友链</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/2019/about">关于我</a>
    </li>
    
  </ul>
</nav>
<nav class="navbar navbar-bottom">
  <ul class="nav">
    <li class="nav-item">
      
      <a class="nav-item-link nav-item-search"  title="搜索">
        <i class="ri-search-line"></i>
      </a>
      
      
      <a class="nav-item-link" target="_blank" href="/atom.xml" title="RSS Feed">
        <i class="ri-rss-line"></i>
      </a>
      
    </li>
  </ul>
</nav>
<div class="search-form-wrap">
  <div class="local-search local-search-plugin">
  <input type="search" id="local-search-input" class="local-search-input" placeholder="Search...">
  <div id="local-search-result" class="local-search-result"></div>
</div>
</div>
    </aside>
    <script>
      if (window.matchMedia("(max-width: 768px)").matches) {
        document.querySelector('.content').classList.remove('on');
        document.querySelector('.sidebar').classList.remove('on');
      }
    </script>
    <div id="mask"></div>

<!-- #reward -->
<div id="reward">
  <span class="close"><i class="ri-close-line"></i></span>
  <p class="reward-p"><i class="ri-cup-line"></i>请我喝杯咖啡吧~</p>
  <div class="reward-box">
    
    
  </div>
</div>
    
<script src="/js/jquery-2.0.3.min.js"></script>


<script src="/js/lazyload.min.js"></script>

<!-- Tocbot -->


<script src="/js/tocbot.min.js"></script>

<script>
  tocbot.init({
    tocSelector: '.tocbot',
    contentSelector: '.article-entry',
    headingSelector: 'h1, h2, h3, h4, h5, h6',
    hasInnerContainers: true,
    scrollSmooth: true,
    scrollContainer: 'main',
    positionFixedSelector: '.tocbot',
    positionFixedClass: 'is-position-fixed',
    fixedSidebarOffset: 'auto'
  });
</script>

<script src="https://cdn.jsdelivr.net/npm/jquery-modal@0.9.2/jquery.modal.min.js"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/jquery-modal@0.9.2/jquery.modal.min.css">
<script src="https://cdn.jsdelivr.net/npm/justifiedGallery@3.7.0/dist/js/jquery.justifiedGallery.min.js"></script>

<script src="/dist/main.js"></script>

<!-- ImageViewer -->

<!-- Root element of PhotoSwipe. Must have class pswp. -->
<div class="pswp" tabindex="-1" role="dialog" aria-hidden="true">

    <!-- Background of PhotoSwipe. 
         It's a separate element as animating opacity is faster than rgba(). -->
    <div class="pswp__bg"></div>

    <!-- Slides wrapper with overflow:hidden. -->
    <div class="pswp__scroll-wrap">

        <!-- Container that holds slides. 
            PhotoSwipe keeps only 3 of them in the DOM to save memory.
            Don't modify these 3 pswp__item elements, data is added later on. -->
        <div class="pswp__container">
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
        </div>

        <!-- Default (PhotoSwipeUI_Default) interface on top of sliding area. Can be changed. -->
        <div class="pswp__ui pswp__ui--hidden">

            <div class="pswp__top-bar">

                <!--  Controls are self-explanatory. Order can be changed. -->

                <div class="pswp__counter"></div>

                <button class="pswp__button pswp__button--close" title="Close (Esc)"></button>

                <button class="pswp__button pswp__button--share" style="display:none" title="Share"></button>

                <button class="pswp__button pswp__button--fs" title="Toggle fullscreen"></button>

                <button class="pswp__button pswp__button--zoom" title="Zoom in/out"></button>

                <!-- Preloader demo http://codepen.io/dimsemenov/pen/yyBWoR -->
                <!-- element will get class pswp__preloader--active when preloader is running -->
                <div class="pswp__preloader">
                    <div class="pswp__preloader__icn">
                        <div class="pswp__preloader__cut">
                            <div class="pswp__preloader__donut"></div>
                        </div>
                    </div>
                </div>
            </div>

            <div class="pswp__share-modal pswp__share-modal--hidden pswp__single-tap">
                <div class="pswp__share-tooltip"></div>
            </div>

            <button class="pswp__button pswp__button--arrow--left" title="Previous (arrow left)">
            </button>

            <button class="pswp__button pswp__button--arrow--right" title="Next (arrow right)">
            </button>

            <div class="pswp__caption">
                <div class="pswp__caption__center"></div>
            </div>

        </div>

    </div>

</div>

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe.min.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/default-skin/default-skin.min.css">
<script src="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe-ui-default.min.js"></script>

<script>
    function viewer_init() {
        let pswpElement = document.querySelectorAll('.pswp')[0];
        let $imgArr = document.querySelectorAll(('.article-entry img:not(.reward-img)'))

        $imgArr.forEach(($em, i) => {
            $em.onclick = () => {
                // slider展开状态
                // todo: 这样不好，后面改成状态
                if (document.querySelector('.left-col.show')) return
                let items = []
                $imgArr.forEach(($em2, i2) => {
                    let img = $em2.getAttribute('data-idx', i2)
                    let src = $em2.getAttribute('data-target') || $em2.getAttribute('src')
                    let title = $em2.getAttribute('alt')
                    // 获得原图尺寸
                    const image = new Image()
                    image.src = src
                    items.push({
                        src: src,
                        w: image.width || $em2.width,
                        h: image.height || $em2.height,
                        title: title
                    })
                })
                var gallery = new PhotoSwipe(pswpElement, PhotoSwipeUI_Default, items, {
                    index: parseInt(i)
                });
                gallery.init()
            }
        })
    }
    viewer_init()
</script>

<!-- MathJax -->

<!-- Katex -->

<!-- busuanzi  -->


<script src="/js/busuanzi-2.3.pure.min.js"></script>


<!-- ClickLove -->

<!-- ClickBoom1 -->

<!-- ClickBoom2 -->

<!-- CodeCopy -->


<link rel="stylesheet" href="/css/clipboard.css">

<script src="https://cdn.jsdelivr.net/npm/clipboard@2/dist/clipboard.min.js"></script>
<script>
  function wait(callback, seconds) {
    var timelag = null;
    timelag = window.setTimeout(callback, seconds);
  }
  !function (e, t, a) {
    var initCopyCode = function(){
      var copyHtml = '';
      copyHtml += '<button class="btn-copy" data-clipboard-snippet="">';
      copyHtml += '<i class="ri-file-copy-2-line"></i><span>COPY</span>';
      copyHtml += '</button>';
      $(".highlight .code pre").before(copyHtml);
      $(".article pre code").before(copyHtml);
      var clipboard = new ClipboardJS('.btn-copy', {
        target: function(trigger) {
          return trigger.nextElementSibling;
        }
      });
      clipboard.on('success', function(e) {
        let $btn = $(e.trigger);
        $btn.addClass('copied');
        let $icon = $($btn.find('i'));
        $icon.removeClass('ri-file-copy-2-line');
        $icon.addClass('ri-checkbox-circle-line');
        let $span = $($btn.find('span'));
        $span[0].innerText = 'COPIED';
        
        wait(function () { // 等待两秒钟后恢复
          $icon.removeClass('ri-checkbox-circle-line');
          $icon.addClass('ri-file-copy-2-line');
          $span[0].innerText = 'COPY';
        }, 2000);
      });
      clipboard.on('error', function(e) {
        e.clearSelection();
        let $btn = $(e.trigger);
        $btn.addClass('copy-failed');
        let $icon = $($btn.find('i'));
        $icon.removeClass('ri-file-copy-2-line');
        $icon.addClass('ri-time-line');
        let $span = $($btn.find('span'));
        $span[0].innerText = 'COPY FAILED';
        
        wait(function () { // 等待两秒钟后恢复
          $icon.removeClass('ri-time-line');
          $icon.addClass('ri-file-copy-2-line');
          $span[0].innerText = 'COPY';
        }, 2000);
      });
    }
    initCopyCode();
  }(window, document);
</script>


<!-- CanvasBackground -->


    
  </div>
</body>

</html>